<?php
class BookTypeController extends Controller{
	//展示所有图书
	public function indexAction(){
		$title="图书类型管理";
		$include_file="alltype";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$typemodel=new TypeModel("tb_booktype");
				//$allbook=$bookmodel->getAllBook();
		//定义每页数量
		$pagesize = 10;
		//接收页数参数,如果为空,则显示第一页
		$current = isset ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		//计算偏移量
		$offset = ($current - 1) * $pagesize;
		//获取总条数,条件where为空
		$total = $typemodel->total("");
		$alltypes = $typemodel->getTypes($offset, $pagesize);
		//载入分页工具类,获取分页信息
		$this->library ( "Page" );
		$page=new Page($total, $pagesize, $current,"index.php",array("c"=>"booktype","a"=>"index"));
		$pageinfo=$page->showPage();
		include VIEWS_PATH."index.html";
	}
	
	//载入添加图书表单
	public function addAction(){
		$title="添加图书类型";
		$include_file="addtype";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		
		include VIEWS_PATH."index.html";
	}
	
	//插入数据
	public function insertAction(){
		$data['typename']=$_POST['typename'];
		$data['days']=$_POST['days'];
		//判断是否为空
		if (empty($data['typename'])||empty($data['days'])) {
			$this->jump("index.php?c=booktype&a=add","图书条形码和图书名不能为空!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$typemodel=new TypeModel("tb_booktype");
		if ($typemodel->insert($data)) {
			$this->jump("index.php?c=booktype&a=index","添加图书类型成功!",2);
		}else {
			$this->jump("index.php?c=booktype&a=add","添加图书类型失败!",3);
		}
	}
	
	//修改图书表单
	public function editAction(){
		$title="修改图书类型";
		$include_file="edittype";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		//强制转为数字
		$pk=$_GET['id']+0;
		$typemodel=new BookModel("tb_booktype");
		$onetype=$typemodel->selectByPk($pk);
		
		
		include VIEWS_PATH."index.html";
	}
	
	//完成图书更新
	public function updateAction(){
		$data['typename']=$_POST['typename'];
		$data['days']=$_POST['days'];
		$data['id']=$_POST['id']+0;
		//判断是否为空
		if (empty($data['typename'])||empty($data['days'])) {
			$this->jump("index.php?c=booktype&a=edit&id={$data['id']}","图书条形码和图书名不能修改为空!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$typemodel=new TypeModel("tb_booktype");
		if ($typemodel->update($data)) {
			$this->jump("index.php?c=booktype&a=index","修改成功!",2);
		}else {
			$this->jump("index.php?c=booktype&a=edit&id={$data['id']}","修改失败!",3);
		}
		
	}
	
	//删除图书
	public function deleteAction(){
		$pk=$_GET['id']+0;
		$typemodel=new TypeModel("tb_booktype");
		if ($typemodel->delete($pk)) {
			$this->jump("index.php?c=booktype&a=index", "删除成功!",2);
		}else {
			$this->jump("index.php?c=booktype&a=index", "删除失败,请刷新页面后重试!",3);
		}
	}
}